package november.search;

import java.util.ArrayList;
import java.util.List;

/**
 * @author huangxin
 * @date 2019/11/19-19:48
 */
public class InsertValueSearch {
    public static void main(String[] args) {
        int[] arr = {1, 2,3,4,5,6,7,8,9,10};
        int index = insertValueSearch(arr, 0, arr.length - 1, 10);
        if (index == -1) {
            System.out.println("没有找到");
        } else {
            System.out.println("下标为" + index);
            System.out.println(arr[index]);
        }

//        System.out.println(binarySearch2(arr, 0, arr.length - 1, 6));
    }


    public static int insertValueSearch(int[] arr, int start, int end, int value) {
        if (start > end || value < arr[start] || value > arr[end]) {
            return -1;
        }

        //自适应中位数
        int mid = start + (end - start) * (value - arr[start]) / (arr[end] - arr[start]);

        if (value > arr[mid]) {
            return insertValueSearch(arr, mid + 1, end, value);
        } else if (value < arr[mid]) {
            return insertValueSearch(arr, start, mid - 1, value);
        } else {
            return mid;
        }
    }


}
